package com.lin.missyou.manager.redis;

import com.lin.missyou.bo.OrderMessageBO;
import com.lin.missyou.service.CouponBackService;
import com.lin.missyou.service.OrderCancelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.stereotype.Component;

/**
 * 监听发送的消息
 * 一个回调接口，消息监听器，用于接收发送到 channel 的消息
 */
@Component
public class TopicMessageListener implements MessageListener {

    @Autowired
    private OrderCancelService orderCancelService;
    @Autowired
    private CouponBackService couponBackService;

    private static ApplicationEventPublisher publisher;

    @Autowired
    public void setPublisher(ApplicationEventPublisher publisher){
        TopicMessageListener.publisher = publisher;
    }


    @Override
    public void onMessage(Message message, byte[] bytes) {
        String expiredKey = new String(message.getBody());
        String topic = new String(message.getChannel());
        System.out.println(expiredKey);
        System.out.println(topic);

        OrderMessageBO orderMessageBO = new OrderMessageBO(expiredKey);
        orderCancelService.cancel(orderMessageBO);
        couponBackService.returnBack(orderMessageBO);
//        TopicMessageListener.publisher.publishEvent(orderMessageBO);
    }
}
